package com.yj.spring.charpter04;

import cn.hutool.core.util.StrUtil;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.util.Iterator;

public class XmlResource implements Resource {

    private String path;

    Document document;

    Element rootElement;

    Iterator iterator;

    public XmlResource(String path) {
        this.path = path;
        this.parse();
    }

    public void parse() {
        try {
            if (StrUtil.isEmpty(path)){
                throw new IllegalArgumentException("path is empty");
            }
            SAXReader saxReader = new SAXReader();
            Document document = saxReader.read(this.getClass().getClassLoader().getResource(path));
            Element rootElement = document.getRootElement();
            iterator = rootElement.elementIterator();
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }


    @Override
    public boolean hasNext() {
        return iterator.hasNext();
    }

    @Override
    public Object next() {
        return iterator.next();
    }
}
